package com.yonyou.crm.sact.taskcard.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.sact.taskcard.entity.SactTaskcardVO;
import com.yonyou.crm.sys.modules.service.BiztypeBizServiceImpl;
import com.yonyou.crm.sys.modules.service.ModuleBizServiceImpl;
import com.yonyou.crm.sys.user.service.UserBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class SactTaskcardBizServiceImpl extends BaseBizServiceImpl<SactTaskcardVO> {
	
	@Autowired
	private EnumBizServiceImpl enumService;
	@Autowired
	private UserBizServiceImpl userService;
	@Autowired
	private BiztypeBizServiceImpl biztypeService;
	@Autowired
	private ModuleBizServiceImpl moduleService;
	
	public List<SactTaskcardVO> getSactTaskcardList(Map<String, Object> paramMap){
		List<SactTaskcardVO> listSactTaskcard = null;
		if(null == paramMap){
			paramMap = new HashMap<String, Object>();
		}
		paramMap.put("tenantId", InvocationInfoProxy.getTenantid());
		if (paramMap != null) {
			listSactTaskcard = dao.selectVoList(SactTaskcardVO.class, "getSactTaskcardList", paramMap);
		}
		transSactTaskcard(listSactTaskcard);
		return listSactTaskcard;
	}

	private List<SactTaskcardVO> transSactTaskcard(List<SactTaskcardVO> sactTaskcardList) {
		if (sactTaskcardList != null && sactTaskcardList.size() > 0) {
			List<FieldRelation<SactTaskcardVO>> relations = new ArrayList<FieldRelation<SactTaskcardVO>>();
			relations.add(new EnumFieldRelation<SactTaskcardVO>(SactTaskcardVO.class, "enableState", "enableStateName", enumService, "enable_state"));
			relations.add(new RefFieldRelation<SactTaskcardVO>(SactTaskcardVO.class, "sysCreatedUserId", new String[]{"userName"}, userService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<SactTaskcardVO>(SactTaskcardVO.class, "mtObjId", new String[]{"mtObjName"}, moduleService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<SactTaskcardVO>(SactTaskcardVO.class, "mtBiztypeId", new String[]{"mtBiztypeName"}, biztypeService, "selectFieldsByIds"));
			new FieldRelationHandler<SactTaskcardVO>().handler(sactTaskcardList, relations);
		}
		return sactTaskcardList;
	}
	
	public SactTaskcardVO insertSactTaskcard(SactTaskcardVO sactTaskcard){
		sactTaskcard = super.saveVO(sactTaskcard);
		
		return sactTaskcard;
	}
	
	private boolean validateRepeatBiztype(Long biztypeId) {
		boolean flag = false;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("biztypeId", biztypeId);
		param.put("tenantId", InvocationInfoProxy.getTenantid());
		List<Map<String, Object>> selectList = dao.selectList(SactTaskcardVO.class, "getTaskCardByBiztypeId", param);
		if (selectList != null && selectList.size() > 0) {
			// 数据库已经存在同类型的任务卡
			flag = true;
		}
		return flag;
	}
	
	@Override
	protected void beforeSaveVO(SactTaskcardVO vo) {
		super.beforeSaveVO(vo);
		boolean hasOne = validateRepeatBiztype(vo.getMtBiztypeId());
		if (hasOne) throw new CrmBusinessException("任务卡业务类型不可重复");
		vo.setIsDefault(new Byte("2"));//TODO 2代表该任务卡是非默认的任务卡
		Date date = new Date();
		vo.setName(vo.getMtBiztypeName());
		vo.setSysCreatedTime(date);
		vo.setSysCreatedUserId(new Long(InvocationInfoProxy.getParameter("id")));
		vo.setEnableState(new Byte("1"));//TODO 1代表该任务卡新增默认是启用的
	}
	
	@Override
	protected void afterSaveVO(SactTaskcardVO vo) {
		// 自动生成的方法存根
		super.afterSaveVO(vo);
		List<SactTaskcardVO> sactTaskcardList = new ArrayList<SactTaskcardVO>();
		sactTaskcardList.add(vo);
		transSactTaskcard(sactTaskcardList);
		vo = sactTaskcardList.get(0);
	}
	
	public int deleteSactTaskcard(Long id){
		SactTaskcardVO sactTaskcard = new SactTaskcardVO();
		sactTaskcard.setId(id);
		return super.deleteVO(sactTaskcard);
	}
	
	public SactTaskcardVO updateSactTaskcard(SactTaskcardVO sactTaskcard) {
		return super.updateVO(sactTaskcard);
	}
	
	@Override
	protected void beforeUpdateVO(SactTaskcardVO vo) {
		super.beforeUpdateVO(vo);
		vo.setName(vo.getMtBiztypeName());
		vo.setSysModifiedTime(new Date());
		vo.setSysModifiedUserId(new Long(InvocationInfoProxy.getParameter("id")));
	}
	
	public int deleteSactTaskcardByIDs(String[] idStrs) {
		return super.batchDeleteByIDs(SactTaskcardVO.class, idStrs);
	}
	
	public SactTaskcardVO getSactcardDetailById(Long id){
		SactTaskcardVO vo = super.getVOById(SactTaskcardVO.class, id);
		List<SactTaskcardVO> sactTaskcardList = new ArrayList<SactTaskcardVO>();
		sactTaskcardList.add(vo);
		transSactTaskcard(sactTaskcardList);
		vo = sactTaskcardList.get(0);
		return vo;
	}
	
	/**
	 * 任务卡翻译
	 * @param ids
	 * @return
	 */
	public Object selectFieldsByIds(Object[] ids) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("ids", ids);
		return dao.selectList(SactTaskcardVO.class, "selectFieldsByIds", para);
	}
	
	/**
	 * 参照列表
	 * @param paraMap
	 * @return
	 */
	public List<SactTaskcardVO> getRefList(Map<String, Object> paraMap) {
		
		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}
		
		paraMap.put("tenantId", InvocationInfoProxy.getTenantid());
		List<SactTaskcardVO> list = dao.selectVoList(SactTaskcardVO.class, "getRefList", paraMap);
		return transSactTaskcard(list);
	}
	
	public void batchUpdateEnableState(String[] idArray, Integer enableState) {
		Byte en = (byte) enableState.intValue();
		Map<String, Object> enableParaMap = new HashMap<String, Object>();
		enableParaMap.put("ids", idArray);
		enableParaMap.put("enableState", en);
		enableParaMap.put("enableUserId", new Long(InvocationInfoProxy.getParameter("id")));
		enableParaMap.put("enableTime", new Date());
		dao.updateSql(SactTaskcardVO.class, "batchUpdateEnableState", enableParaMap);
	}
}
